package com.eightbears.beboy.ui.user.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.CommonAPI;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.SharesUtil;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.entity.CodeEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_INVITE_CODE;

/**
 * 我要先进
 */
public class TakeMoneyActivity extends BaseActivity {
    @BindView(R.id.ll_back)
    LinearLayout mLlBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.rl_title_root)
    RelativeLayout mRlTitleRoot;
    @BindView(R.id.tv_right_img_btn)
    TextView mTvRightImgBtn;
    @BindView(R.id.tv_save_address)
    TextView mTvSaveAddress;
    @BindView(R.id.tv_submit)
    TextView mTvSubmit;
    @BindView(R.id.iv_right_icon)
    ImageView mIvRightIcon;
    @BindView(R.id.ll_right)
    LinearLayout mLlRight;
    @BindView(R.id.progressBar)
    ProgressBar mProgressBar;
    @BindView(R.id.iv_friend)
    ImageView mIvFriend;
    @BindView(R.id.tv_num)
    TextView tv_num;
    @BindView(R.id.tv_invite)
    TextView mTvInvite;
    @BindView(R.id.fl_line)
    FrameLayout mFlLine;
    @BindView(R.id.tv_rule1)
    TextView mTvRule1;
    @BindView(R.id.tv_rule2)
    TextView mTvRule2;
    @BindView(R.id.tv_rule3)
    TextView mTvRule3;
    @BindView(R.id.activity_agreement)
    RelativeLayout mActivityAgreement;
    @BindView(R.id.sw_finish)
    SwipeBackLayout mSwFinish;
    private CodeEntity mCodeEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_money);
        ButterKnife.bind(this);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        mTvTitle.setText(getString(R.string.text_my_money));
    }

    @Override
    protected void initData() {
        getData();
    }

    @Override
    protected void initEvent() {
        mSwFinish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

    @OnClick(R.id.tv_invite)
    void invite() {
        SharesUtil.sharedInviteBoard(TakeMoneyActivity.this, mCodeEntity);
    }

    /**
     *
     */
    private void getData() {
        OkHttpUtils.get().url(URL_INVITE_CODE)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .build().execute(new StringCallback() {

            @Override
            public void onResponse(String response) {
                KLog.e(response);
                mCodeEntity = GsonUtil.fromJson(response, CodeEntity.class);
                //如果登录成功
                if (null != mCodeEntity && mCodeEntity.getStatus_code() == Integer.parseInt(CommonAPI.CONNECTION_STATUS_SUCCESS)) {
                    tv_num.setText(getString(R.string.text_invite_code_show) + String.valueOf(mCodeEntity.getData().getCode()));
                }
            }

            @Override
            public void onError(Call request, Exception e) {
                // 完成请求失败的各种错误提示
                KLog.e(e.getMessage());
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError((Activity) mContext, status_error);
            }
        });
    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
